package com.wnhoo.smartfishtank.api;

import android.util.Log;

import com.google.gson.Gson;
import com.wnhoo.smartfishtank.api.data.LM_DataAddDelEdit;
import com.wnhoo.smartfishtank.api.data.LM_DataHistory;
import com.wnhoo.smartfishtank.api.data.LM_DataHistoryCount;
import com.wnhoo.smartfishtank.api.data.LM_DataPlugList;
import com.wnhoo.smartfishtank.api.data.LM_DataReceive;
import com.wnhoo.smartfishtank.api.data.LM_DataSend;
import com.wnhoo.smartfishtank.api.data.LM_DataStatus;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import java.io.IOException;

/**
 * Created by kevinwen on 2014/12/26.
 * Copyright (c) 2014/12/26.KevinWen. All rights reserved.
 */
public class LM_PlugAPIs {

    private static final Gson gson;
    private static final String SENSOR_ID;
    private static final String SENSOR_TYPE;
    public static final int TYPE_DEVICE;
    private static String tag = "LM_PlugAPIs";

    static {
        gson = new Gson();
        SENSOR_ID = "01";
        SENSOR_TYPE = "1";
        TYPE_DEVICE = 1005;
    }

//    public static LM_DataSend requestSendControl(boolean isDebug,String deviceId,
//                                              String data,String token_value) throws ClientProtocolException, IOException ,JSONException{
//        String sensorId = "S01";
//        String result = LM_BasicAPI.requestSend(isDebug,deviceId,sensorId,SENSOR_TYPE,data,token_value);
//
//        return gson.fromJson(result,LM_DataSend.class);
//    }

    /**
     * 鱼缸温控器的发送命令
     *
     * @param isDebug
     * @param deviceId
     * @param data
     * @param token_value
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     * @throws JSONException
     */
    //开关 0，温控器关机；1，设置50%功率；2，设置100%功率；-1，查询温控器全状态指令（温控器回复S13指令代码）。
    public static LM_DataSend requestSendControl(boolean isDebug, String deviceId,
                                                 String data, String token_value) throws ClientProtocolException, IOException, JSONException {
        String sensorId = "S12";
        String result = LM_BasicAPI.requestSend(isDebug, deviceId, sensorId, SENSOR_TYPE, data, token_value);
        Log.i("requestSendControl", "requestSendControl -- S12!!!!");
        return gson.fromJson(result, LM_DataSend.class);
    }

    //水温设置
    public static LM_DataSend requestSendTemperature(boolean isDebug, String deviceId,
                                                     String data, String token_value) throws ClientProtocolException, IOException, JSONException {
        String sensorId = "S13";
        String result = LM_BasicAPI.requestSend(isDebug, deviceId, sensorId, SENSOR_TYPE, data, token_value);
        return gson.fromJson(result, LM_DataSend.class);
    }

//    //温控器的全状态
////    数据 “1,25,27,3,0”
//    //    表示：1为温控器处在功率为50%的状态。25表示设定的水的温度。27表示当前鱼缸水的温度。0表示偏差的温度值不需要预警(无正负区分，1表示需要预警)。1,表示温控器正在加热中。0表示鱼缸有水不需要预警（1表示需要预警）。
////    注：1表示要预警，0表示不预警。
//    public static LM_DataSend requestSendWholeState(boolean isDebug, String deviceId,
//                                                    String data, String token_value) throws ClientProtocolException, IOException, JSONException {
//        String sensorId = "S14";
//        String result = LM_BasicAPI.requestSend(isDebug, deviceId, sensorId, SENSOR_TYPE, data, token_value);
//        return gson.fromJson(result, LM_DataSend.class);
//    }

    //设置温控器水温偏差  3表示正负偏差3°C需要报警。5表示正负偏差5°C需要报警
//    注：3°C偏差为默认
    public static LM_DataSend requestSendTemperatureDifference(boolean isDebug, String deviceId,
                                                               String data, String token_value) throws ClientProtocolException, IOException, JSONException {
        String sensorId = "S15";
        String result = LM_BasicAPI.requestSend(isDebug, deviceId, sensorId, SENSOR_TYPE, data, token_value);

//        Log.i(tag,"result"+result);
        return gson.fromJson(result, LM_DataSend.class);
    }

//    //设备报错，/S16/1/1  1，表示温度计出错。2，表示功率器报错。
//    public static LM_DataSend requestSendDeviceError(boolean isDebug, String deviceId,
//                                                     String data, String token_value) throws ClientProtocolException, IOException, JSONException {
//        String sensorId = "S16";
//        String result = LM_BasicAPI.requestSend(isDebug, deviceId, sensorId, SENSOR_TYPE, data, token_value);
//        return gson.fromJson(result, LM_DataSend.class);
//    }

    /**
     * 鱼缸灯的控制发送命令
     *
     * @param isDebug
     * @param deviceId
     * @param data
     * @param token_value
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     * @throws JSONException
     */
    //鱼缸灯的颜色设置
    public static LM_DataSend requestSendLightSetcolor(boolean isDebug, String deviceId,
                                                       String data, String token_value) throws ClientProtocolException, IOException, JSONException {
        String sensorId = "S15";
        String result = LM_BasicAPI.requestSend(isDebug, deviceId, sensorId, SENSOR_TYPE, data, token_value);
        return gson.fromJson(result, LM_DataSend.class);
    }

//    //鱼缸灯的设备报错
//    public static LM_DataSend requestSendLightError(boolean isDebug, String deviceId,
//                                                       String data, String token_value) throws ClientProtocolException, IOException, JSONException {
//        String sensorId = "S21";
//        String result = LM_BasicAPI.requestSend(isDebug, deviceId, sensorId, SENSOR_TYPE, data, token_value);
//        return gson.fromJson(result, LM_DataSend.class);
//    }


    public static LM_DataSend requestSendText(boolean isDebug, String deviceId,
                                              String data, String token_value) throws ClientProtocolException, IOException, JSONException {
        String sensorId = "S17";
        String result = LM_BasicAPI.requestSend(isDebug, deviceId, sensorId, SENSOR_TYPE, data, token_value);

        return gson.fromJson(result, LM_DataSend.class);
    }

    public static LM_DataSend requestSendNightMode(boolean isDebug, String deviceId,
                                                   String data, String token_value) throws ClientProtocolException, IOException, JSONException {
        String sensorId = "S02";
        String result = LM_BasicAPI.requestSend(isDebug, deviceId, sensorId, SENSOR_TYPE, data, token_value);

        return gson.fromJson(result, LM_DataSend.class);
    }

    public static LM_DataReceive requestReceiveNightMode(boolean isDebug, String deviceId,
                                                         String token_value) throws ClientProtocolException, IOException, JSONException {
        String sensorId = "S02";
        String result = LM_BasicAPI.requestReceive(isDebug, deviceId, sensorId, SENSOR_TYPE, token_value);
        return gson.fromJson(result, LM_DataReceive.class);
    }

    public static LM_DataSend requestSendThermostatMode(boolean isDebug, String deviceId,
                                                        String data, String token_value) throws ClientProtocolException, IOException, JSONException {
        String sensorId = "S60";
        String result = LM_BasicAPI.requestSend(isDebug, deviceId, sensorId, SENSOR_TYPE, data, token_value);

        return gson.fromJson(result, LM_DataSend.class);
    }

    public static LM_DataReceive requestReceiveThermostatMode(boolean isDebug, String deviceId,
                                                              String token_value) throws ClientProtocolException, IOException, JSONException {
        String sensorId = "S60";
        String result = LM_BasicAPI.requestReceive(isDebug, deviceId, sensorId, SENSOR_TYPE, token_value);
        return gson.fromJson(result, LM_DataReceive.class);
    }

    public static LM_DataReceive requestReceiveData(boolean isDebug, String deviceId,
                                                    String sensorId, String sensorType, String token_value) throws ClientProtocolException, IOException, JSONException {

        String result = LM_BasicAPI.requestReceive(isDebug, deviceId, sensorId, sensorType, token_value);
        return gson.fromJson(result, LM_DataReceive.class);

    }

    public static LM_DataStatus requestOnlineStatus(boolean isDebug, String deviceId, String token_value) throws ClientProtocolException, IOException, JSONException {

        String result = LM_BasicAPI.deviceOnlineStatus(isDebug, deviceId, token_value);
        return gson.fromJson(result, LM_DataStatus.class);
    }

    public static LM_DataAddDelEdit deviceAdd(boolean isDebug, String deviceId, int type, String deviceTitle, String token_value) throws ClientProtocolException, IOException {

        String result = LM_BasicAPI.deviceAdd(isDebug, deviceId, type, deviceTitle, token_value);
        return gson.fromJson(result, LM_DataAddDelEdit.class);
    }

    public static LM_DataAddDelEdit deviceDel(boolean isDebug, String deviceId, String token_value) throws ClientProtocolException, IOException {

        String result = LM_BasicAPI.deviceDel(isDebug, deviceId, token_value);
        return gson.fromJson(result, LM_DataAddDelEdit.class);
    }

    public static LM_DataAddDelEdit deviceEdit(boolean isDebug, String deviceId, int type, String title, String token_value) throws ClientProtocolException, IOException {

        String result = LM_BasicAPI.deviceEdit(isDebug, deviceId, type, title, token_value);
        return gson.fromJson(result, LM_DataAddDelEdit.class);
    }


    public static LM_DataHistory history(boolean isDebug, String deviceId, String sensor_id, int page, String token_value) throws ClientProtocolException, IOException {
        int sensor_type = 1;
        String result = LM_BasicAPI.history(isDebug, deviceId, sensor_id, sensor_type, page, token_value);
        return gson.fromJson(result, LM_DataHistory.class);
    }

    public static LM_DataHistoryCount historyCount(boolean isDebug, String deviceId, String sensor_id, String token_value) throws ClientProtocolException, IOException {
        int sensor_type = 1;
        String result = LM_BasicAPI.historyCount(isDebug, deviceId, sensor_id, sensor_type, token_value);
        return gson.fromJson(result, LM_DataHistoryCount.class);
    }

    /**
     * 请求所有设备的状态
     *
     * @param isDebug
     * @param token_value
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static LM_DataPlugList deviceList(boolean isDebug, String token_value) throws ClientProtocolException, IOException {

        int page = 0;
        int TYPE_DEVICE = 0;
        String result = LM_BasicAPI.deviceList(isDebug, token_value, TYPE_DEVICE, page);
        return gson.fromJson(result, LM_DataPlugList.class);
    }
}
